package com.ycy.chuwenuniapp.utils;

import cn.hutool.core.util.StrUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CommonUtils {
    public static String getNowTimeStr() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    public static int compareOffsetDate(String DATE1, String DATE2, Integer offsetHour) {
        //offsetHour = -2就是前两小时
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt2);
            calendar.add(Calendar.HOUR_OF_DAY, offsetHour);  //前两小时
            Date offsetTime = calendar.getTime();

            return dt1.compareTo(offsetTime);  //等于0日期相等， dt1在前值为负,dt2在前值为正
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static int compareDate(String DATE1, String DATE2) {
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.CHINA);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            // System.out.println("现在时间-----------------" + dt1);
            // System.out.println("时间  -----------------" + dt2);
            return dt1.compareTo(dt2);  //等于0日期相等， dt1在前值为负,dt2在前值为正
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String validateIdentityIdPhoneQQ(String identityId, String phone, String QQ) {
        boolean isErrorIdentityId = false;
        boolean isErrorPhone = false;
        boolean isErrorQQ = false;

        if (StrUtil.isNotBlank(identityId))
            isErrorIdentityId = !(identityId.matches("^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$"));
        if (StrUtil.isNotBlank(phone))
            isErrorPhone = !(phone.matches("^(?:(?:\\+|00)86)?1[3-9]\\d{9}$"));
        if (StrUtil.isNotBlank(QQ))
            isErrorQQ = !(QQ.matches("^[1-9][0-9]{4,12}$"));

        if (!isErrorIdentityId && !isErrorPhone && !isErrorQQ) return "";
        else {
            String message = "";
            if (isErrorIdentityId) message = "身份证号";
            if (isErrorPhone) message += "手机号";
            if (isErrorQQ) message += "QQ号";
            return message + "不合法";
        }
    }
}
